package models

import (
	"social-app/dao"
	"time"
)

type DynamicReply struct {
	Id         int       `json:"id"`
	UserId     int       `json:"user_id"`
	DynamicId  int       `json:"dynamic_id"`
	Content    string    `json:"content"`
	CreateTime time.Time `json:"create_time" gorm:"AutoCreateTime"`
	UpdateTime time.Time `json:"update_time" gorm:"AutoUpdateTime"`
}

type DynamicReplyResponse struct {
	DynamicReply
	Username string `json:"username"`
	Avatar   string `json:"avatar"`
}

func (dr *DynamicReply) TableName() string {
	return "dynamic_reply"
}

func CreateDynamicReply(userId int, dynamicId int, content string) (*DynamicReply, error) {
	dynamicReply := &DynamicReply{
		UserId:    userId,
		DynamicId: dynamicId,
		Content:   content,
	}
	err := dao.Db.Create(dynamicReply).Error
	return dynamicReply, err
}

func GetDynamicReplyByDynamicId(dynamicId int, page int, limit int, order string) ([]DynamicReplyResponse, error) {
	var dynamicReplyList []DynamicReplyResponse
	model := dao.Db.Limit(limit).Offset((page - 1) * limit)
	model = model.Where("dynamic_id = ?", dynamicId)
	model = model.Joins("inner join user on user.id = dynamic_reply.user_id")
	model = model.Joins("inner join user_profile on user.id = user_profile.user_id")
	model = model.Select("dynamic_reply.*, user.username, user_profile.avatar")
	model = model.Order(order)
	err := model.Find(&dynamicReplyList).Error
	return dynamicReplyList, err
}
